<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\InputGroup;
use App\Http\Requests\InputGroupRequest;

class InputGroupController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $groups = InputGroup::withCount('items')->get();
        return view('admin.inputgroup.index', ['datas' => $groups]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.inputgroup.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(InputGroupRequest $request)
    {
        InputGroup::create($request->all());
        return redirect()->route('admin.inputgroup.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(InputGroup $inputgroup)
    {
        return view('admin.inputgroup.create', ['data' => $inputgroup]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(InputGroupRequest  $request, InputGroup $inputgroup)
    {
        $inputgroup->update($request->all());
        return redirect()->route('admin.inputgroup.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(InputGroup $inputgroup)
    {
        if($inputgroup->items()->exists()) {
            return response()->json(['msg' => 'pls delete the items of the group'], 400);
        }
        $inputgroup->delete();
        return response()->json(['success' => 'ok']);
    }
}
